package com.key.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.key.model.UserData;

import android.annotation.SuppressLint;
import android.media.MediaRecorder;
import android.media.MediaRecorder.AudioSource;
import android.media.MediaRecorder.OutputFormat;

@SuppressLint("SdCardPath")
public class Listener {

	public static MediaRecorder mr = new MediaRecorder();
	String strPath = "";
	String strMediaType = "";
	
	public Listener( String _strPath,String _strMideaType ) {
		strPath = _strPath;
		strMediaType = _strMideaType;
	}
	
	void setSavePath( String _strPath ) {
		strPath = _strPath;
	}
	
	void setMediaType( String _strMediaType ) {
		strMediaType = _strMediaType;
	}
	
	public void start() {
		try {
			if (UserData.mediaRecorder != null) {
				UserData.mediaRecorder.release();
			}
			if (mr != null) {
				mr.release();
				mr = new MediaRecorder();
			}
		} catch (Exception e) {
			//Contants.writeFileToSD("[0001]" + e.toString());
		}
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");     
		String date = sDateFormat.format(new java.util.Date());  
		
		mr.setAudioSource(AudioSource.MIC);
		mr.setOutputFormat(OutputFormat.THREE_GPP);
		mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		
		String strSavePath = strPath;
		mr.setOutputFile( strSavePath + date + strMediaType); //".amr");
		
		try {
			mr.prepare();
			mr.start();
			//Contants.writeFileToSD(date + "开始录音\n");
		} catch(Exception e) {
			//Contants.writeFileToSD("[0002]" + e.toString());
		}
	}
	
	public void end() {
		
		try {
			mr.stop();
			mr.release();
			//Contants.writeFileToSD(new Date().toLocaleString() + "结束录音");
		} catch(Exception e) {
			//Contants.writeFileToSD("[0003]" + e.toString());
		}
	}
	
}
